package com.fbs.wowo.chat.mapper;

import com.fbs.wowo.chat.entity.Friend;
import com.fbs.wowo.chat.vo.MyFriend;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface FriendMapper {
    @Delete({
        "delete from friend",
        "where mid = #{mid,jdbcType=VARCHAR}",
          "and fid = #{fid,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(@Param("mid") String mid, @Param("fid") String fid);

    @InsertProvider(type=FriendSqlProvider.class, method="insertSelective")
    int insertSelective(Friend record);

    @UpdateProvider(type=FriendSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Friend record);

    /**
     * 按id获取好友关系，无论是否验证
     * @param mid 我的user id
     * @param fid 对方user id
     * @return
     */
    @Select({
            "select",
            "f.mid, f.fid, f.my_group, f.f_group, f.my_nickname, f.f_nickname, f.valid, u.name, u.brief, u.photo, u.sex",
            "from friend f",
            "inner join user u",
            "on u.user_id=f.fid",
            "where mid = #{mid,jdbcType=VARCHAR}",
            "and fid = #{fid,jdbcType=VARCHAR}",
            "union all",
            "select",
            "f.mid, f.fid, f.my_group, f.f_group, f.my_nickname, f.f_nickname, f.valid, u.name, u.brief, u.photo, u.sex",
            "from friend f",
            "inner join user u",
            "on u.user_id=f.mid",
            "where mid = #{fid, jdbcType=VARCHAR}",
            "and fid = #{mid, jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="photo", property="photo", jdbcType=JdbcType.VARCHAR),
            @Result(column="brief", property="brief", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="sex", jdbcType=JdbcType.TINYINT),
            @Result(column="mid", property="mid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="fid", property="fid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="my_group", property="myGroup", jdbcType=JdbcType.VARCHAR),
            @Result(column="f_group", property="fGroup", jdbcType=JdbcType.VARCHAR),
            @Result(column="my_nickname", property="myNickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="f_nickname", property="fNickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT)
    })
    Friend selectByPrimaryKey(@Param("mid") String mid, @Param("fid") String fid);

    /**
     * 好友列表
     *
     * TODO 假如模块分开，不能详细控制别的对象细节，valid等字段需要筛选怎么办？
     * @param id
     * @return
     */
    @Select({
            "select",
            "u.id, t.uid, u.name, u.photo, t.fgroup, t.nickname",
            "from user u",
            "inner join (",
            "select",
            "fid as uid, my_group as fgroup, my_nickname as nickname",
            "from friend",
            "where mid = #{id,jdbcType=VARCHAR} and valid = 1",
            "union all",
            "select",
            "mid as uid, f_group as fgroup, f_nickname as nickname",
            "from friend",
            "where fid = #{id,jdbcType=VARCHAR} and valid = 1",
            ") t",
            "where u.user_id = t.uid"
    })
    @Results({
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="mid", property="mid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="fid", property="fid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="my_group", property="myGroup", jdbcType=JdbcType.VARCHAR),
            @Result(column="f_group", property="fGroup", jdbcType=JdbcType.VARCHAR),
            @Result(column="my_nickname", property="myNickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="f_nickname", property="fNickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT)
    })
    List<MyFriend> selectAll(String id);

    /**
     * 按id获取好友关系（已通过验证，未拉黑）
     * @param mid
     * @param fid
     * @return
     */
    @Select({
            "select",
            "f.mid, f.fid, f.my_group, f.f_group, f.my_nickname, f.f_nickname, f.valid, u.name, u.brief, u.photo, u.sex",
            "from friend f",
            "inner join user u",
            "on u.user_id=f.fid",
            "where mid = #{mid,jdbcType=VARCHAR}",
            "and fid = #{fid,jdbcType=VARCHAR} and valid=1",
            "union all",
            "select",
            "f.mid, f.fid, f.my_group, f.f_group, f.my_nickname, f.f_nickname, f.valid, u.name, u.brief, u.photo, u.sex",
            "from friend f",
            "inner join user u",
            "on u.user_id=f.mid",
            "where mid = #{fid, jdbcType=VARCHAR}",
            "and fid = #{mid, jdbcType=VARCHAR} and valid=1"
    })
    @Results({
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="photo", property="photo", jdbcType=JdbcType.VARCHAR),
            @Result(column="brief", property="brief", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="sex", jdbcType=JdbcType.TINYINT),
            @Result(column="mid", property="mid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="fid", property="fid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="my_group", property="myGroup", jdbcType=JdbcType.VARCHAR),
            @Result(column="f_group", property="fGroup", jdbcType=JdbcType.VARCHAR),
            @Result(column="my_nickname", property="myNickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="f_nickname", property="fNickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT)
    })
    Friend selectByPrimaryKeyValid(@Param("mid") String mid, @Param("fid") String fid);

}